package cn.springboot.erp.service.impl;

import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.Categories;
import cn.springboot.erp.mapper.CategoriesMapper;
import cn.springboot.erp.vo.OrderVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.springboot.erp.entity.PurchaseOrderDetails;
import cn.springboot.erp.service.PurchaseOrderDetailsService;
import cn.springboot.erp.mapper.PurchaseOrderDetailsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author asus
* @description 针对表【purchase_order_details】的数据库操作Service实现
* @createDate 2024-05-13 13:50:58
*/
@Service
public class PurchaseOrderDetailsServiceImpl extends ServiceImpl<PurchaseOrderDetailsMapper, PurchaseOrderDetails>
    implements PurchaseOrderDetailsService{

    @Resource
    private CategoriesMapper categoriesMapper;
    @Override
    public ResponseResult listPage(QueryParams<PurchaseOrderDetails> queryParams) {
        PurchaseOrderDetails searchParams = queryParams.getSearchParams();
        Integer limit = queryParams.getLimit();
        Integer page = queryParams.getPage();
        Page<PurchaseOrderDetails> detailsPage = new Page<>(page, limit);
        LambdaQueryWrapper<PurchaseOrderDetails> lambda = new QueryWrapper<PurchaseOrderDetails>().lambda();
        lambda.eq(StringUtils.isNotBlank(searchParams.getSupplierName()),PurchaseOrderDetails::getSupplierName,searchParams.getSupplierName())
                .eq(StringUtils.isNotBlank(searchParams.getName()),PurchaseOrderDetails::getName,searchParams.getName())
                .orderByDesc(PurchaseOrderDetails::getCreateTime);
        baseMapper.selectPage(detailsPage,lambda);

        List<OrderVO> vo = detailsPage.getRecords().stream().map(PurchaseOrderDetails -> {
            OrderVO orderVO = new OrderVO();
            BeanUtils.copyProperties(PurchaseOrderDetails, orderVO);
            Integer categoryId = PurchaseOrderDetails.getCategoryId();
            Categories categories = categoriesMapper.selectById(categoryId);
            orderVO.setCategoryName(categories.getName());
            return orderVO;
        }).collect(Collectors.toList());
        return ResponseResult.Success()
                .data("total",detailsPage.getTotal())
                .data("list",vo);
    }
}




